package regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA
 * Description:正则反向引用
 * User:22954
 * Date:2024-06-06
 * Time:15:38
 */
public class Rule7 {
    /*
    \\i----内部反向引用,表示与捕获的第i个分组一样内容
    $i----外部反向引用
     */
    public static void main(String[] args) {
        String content = "hello1234 world99 kk8888 no1221";
        String regex = "(\\d)\\1";
        regex = "(\\d)\\1{3}";
        regex = "(\\d)(\\d)\\2\\1";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println(matcher.group(0));
        }

        //去结巴程序
        content = "I...I'mmm a bbbb...o......y";
        regex = "\\.";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(content);
        content = matcher.replaceAll("");
        regex = "(.)\\1+";
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(content);
        content = matcher.replaceAll("$1");
        System.out.println(content);//I'm a boy
    }
}
